/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2015年4月9日         luwei
 */

package com.coocaa.x.app.framework.utils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Stack;

import android.content.Context;

import com.coocaa.x.app.framework.app.LiteAppApplication;
import com.coocaa.x.framework.app.CoocaaApplication;
import com.coocaa.x.framework.app.CoocaaApplication.CoocaaVersion;
import com.coocaa.x.framework.app.CoocaaApplication.DeviceModeMidType;

public class ConfigUtils {
    private static final String CONFIG_PATH = "config";
    private static final String COMMON = "common";

    public static InputStream getCurrnetConfig(String name, Context context) throws Exception {
        Stack<String> configPath = new Stack<String>();
        configPath.push(CONFIG_PATH);
        String channel = LiteAppApplication.getDeviceChannel();
        boolean find = false;
        String currentChannelDir = "";
        find = findDir(configPath, channel, context);// 检测是否有此渠道的配置
        if (!find) {
            // 无此渠道的配置使用默认配置
            currentChannelDir = COMMON;
            configPath.push(currentChannelDir);
            configPath.push(name);
            return context.getAssets().open(generatePath(configPath));
        } else {
            currentChannelDir = configPath.get(1);
        }
        find = false;
        DeviceModeMidType modeMidType = CoocaaApplication.getDeviceModeMidType();
        String type = modeMidType.skytype;
        find = findDir(configPath, type, context);// 检测是否有此机型的配置
        if (!find) {
            // 无此机型配置使用此渠道下的默认配置
            configPath.push(COMMON);
        }
        find = false;
        CoocaaVersion coocaaVersion = null;
        try {
            coocaaVersion = CoocaaApplication.getCoocaaVersion();
        } catch (Exception e) {
            coocaaVersion = new CoocaaVersion();
            coocaaVersion.major = -1;
        }
        find = findDir(configPath, String.valueOf(coocaaVersion.major) + ".x", context);// 检测是否有此版本的配置
        if (!find) {
            // 无此版本的配置回退到此渠道的common配置
            configPath.pop();
            configPath.push(COMMON);
            findDir(configPath, String.valueOf(coocaaVersion.major) + ".x", context);
        }
        find = false;
        find = findFile(configPath, name, context);// 检测是否有配置文件
        if (!find) {
            // 无配置文件回退到此渠道的common配置
            configPath.pop();
            configPath.pop();
            configPath.push(currentChannelDir);
            find = findDir(configPath, String.valueOf(coocaaVersion.major) + ".x", context);// 检测是否有此版本的配置，正常情况下都存在
            configPath.push(name);
        }
        return context.getAssets().open(generatePath(configPath));
    }

    public static boolean findFile(Stack<String> configPath, String channel, Context context)
            throws IOException {
        String[] dirPath = context.getAssets().list(generatePath(configPath));
        for (String dir : dirPath) {
            if (dir.contains(channel.toLowerCase())) {
                configPath.push(dir);
                return true;
            }
        }
        return false;
    }

    public static boolean findDir(Stack<String> configPath, String channel, Context context)
            throws IOException {
        String[] dirPath = context.getAssets().list(generatePath(configPath));
        for (String dir : dirPath) {

            String[] containChannels = dir.toLowerCase().split("_");
            for (String containChannel : containChannels) {
                if (containChannel.equals(channel.toLowerCase())) {
                    configPath.push(dir);
                    return true;
                }
            }
        }
        return false;
    }

    private static String generatePath(Stack<String> paths) {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < paths.size(); i++) {
            builder.append(paths.get(i));
            if (i < paths.size() - 1) {
                builder.append(File.separator);
            }
        }
        return builder.toString();
    }
}
